package com.dangdang.action.catalog;

import java.util.List;
import java.util.Map;

import com.dangdang.action.BaseAction;
import com.dangdang.domain.Book;
import com.dangdang.domain.Category;
import com.dangdang.domain.Product;
import com.dangdang.service.CatalogService;
import com.dangdang.service.ServiceFactory;
import com.dangdang.util.Constants;

/**
 * 图书类表控制层
 * 
 * @author Wangpeihu
 * @team [Never give up]
 * @since JDK1.6(建议)
 * @date 2010-1-25
 */
public class BookListAction extends BaseAction {

	/** param 每页显示的数量 */
	private int rowsPerPage;

	/** 二级分类ID */
	private Integer cTwo = 1;

	/** 三级分类ID */
	private Integer cThree;

	/** 需要显示页数 初始值为1 */
	private int page = 1;

	/** output 所有符合查询条件的类别 */
	private List<Category> catList;

	/** output 所有符合查询条件的图书 */
	private List<Product> bookList;

	/** 共需要多少页 */
	private int totalPage;

	/** 排序方式,默认是1 */
	private int order = 1;

	/** 详细商品的ID */
	private Integer productId;

	/** 图书详细信息 */
	private Book productDetail;

	/** 排序条件 */
	private Map<Integer, String> orderOptions;

	private CatalogService cs;

	/** 搜索关键字 */
	private String likeKey;

	public Integer getProductId() {
		return productId;
	}

	public void setProductId(Integer productId) {
		this.productId = productId;
	}

	public Book getProductDetail() {
		return productDetail;
	}

	public void setProductDetail(Book productDetail) {
		this.productDetail = productDetail;
	}

	public Map<Integer, String> getOrderOptions() {
		return this.orderOptions;
	}

	public void setOrderOptions(Map<Integer, String> orderOptions) {
		this.orderOptions = orderOptions;
	}

	public Integer getcTwo() {
		return this.cTwo;
	}

	public void setcTwo(Integer cTwo) {
		this.cTwo = cTwo;
	}

	public Integer getcThree() {
		return this.cThree;
	}

	public void setcThree(Integer cThree) {
		this.cThree = cThree;
	}

	public Integer getCThree() {
		return cThree;
	}

	public int getOrder() {
		return this.order;
	}

	public void setOrder(int order) {
		this.order = order;
	}

	public List<Category> getCatList() {
		return catList;
	}

	public void setCatList(List<Category> catList) {
		this.catList = catList;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public void setCThree(Integer three) {
		cThree = three;
	}

	public Integer getCTwo() {
		return cTwo;
	}

	public void setCTwo(Integer two) {
		cTwo = two;
	}

	public List<Product> getBookList() {
		return bookList;
	}

	public void setBookList(List<Product> bookList) {
		this.bookList = bookList;
	}

	public int getRowsPerPage() {
		return rowsPerPage;
	}

	public void setRowsPerPage(int rowsPerPage) {
		this.rowsPerPage = rowsPerPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public String getLikeKey() {
		return likeKey;
	}

	public void setLikeKey(String likeKey) {
		this.likeKey = likeKey;
	}

	/**
	 * 显示图书详细信息
	 * 
	 * @return
	 */
	public String showBookDetail() {
		if (productId != null) {
			productDetail = cs.findBookById(productId);
			return Constants.FORWARD_SUCCESS;
		} else {
			return Constants.FORWARD_FAIL;
		}

	}

	/**
	 * 控制方法
	 * 
	 * @return
	 */

	public String execute() {

		cs = ServiceFactory.getCatalogService();

		catList = cs.findCategoryByParentId(cTwo);

		// 获得排序条件
		orderOptions = cs.getMessageOrderOptions();

		// 判断是否提交了三级分类
		Integer catId = cThree != null ? cThree : cTwo;

		// 根据当前类别ID,指定的页数,指定的每页显示的条目数查找 指定的图书，得到的结果是已经排序好的
		bookList = cs.findProductByCategoryId(catId, page, rowsPerPage, order);

		// 根据当前类别ID,每夜显示的条目书,查看一共需要多少页来显示
		totalPage = cs.getProductTotalPageByCategoryId(catId, rowsPerPage);

		// 成功返回
		return Constants.FORWARD_SUCCESS;
	}

	/**
	 * 根据
	 * 
	 * @return
	 */
	public String findProductByLikeKey() {
		cs = ServiceFactory.getCatalogService();
		catList = cs.findCategoryByParentId(cTwo);
		orderOptions = cs.getMessageOrderOptions();
		bookList = cs.findProductByLikeKey(likeKey);
		totalPage = cs.getProductTotalPageByCategoryId(2, rowsPerPage);
		return Constants.FORWARD_SUCCESS;

	}

}
